export default class BaseService {
  constructor() {
    this.baseUrl = "https://mini.ndjjd.top";
    // this.baseUrl = "http://localhost:9000";
    this.initRequest();
  }
  initRequest() {
    ["get", "post", "put", "delete", "patch"].forEach((key) => {
      this[key] = (url, params) => {
        let paramAry = [];
        if (["get", "put", "delete", "patch"].includes(key)) {
          Object.keys(params).forEach((name) => {
            paramAry.push(name + "=" + params[name]);
          });
        }
        let paramUrl = "";
        if (paramAry.length > 0) {
          paramUrl = "?" + paramAry.join("&");
        }
        const ajaxUrl = this.baseUrl + url + paramUrl;
        return new Promise((resolve, reject) => {
          wx.request({
            url: ajaxUrl,
            method: key,
            success: (res) => {
              resolve(res.data);
            },
            fail: (err) => {
              reject(err);
            },
          });
        });
      };
    });
  }
}
